<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>chapter9-1</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li class="active"><a href="/category/cpp.html">cpp</a></li>
                    <li><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->
<section id="content" class="body">
  <article>
    <header>
      <h1 class="entry-title">
        <a href="/chapter9-1.html" rel="bookmark"
           title="Permalink to chapter9-1">chapter9-1</a></h1>
    </header>

    <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/cpp.html">cpp</a><a href="/tag/progarmming.html">progarmming</a></span>
</footer><!-- /.post-info -->      <h1>结构体类型</h1>
<p>一、结构体类型
将逻辑相关，不同的数据类型放在一个新的数据类型中，即定义新的数据类型。
1. 构造结构类型
* struct 结构体类型名{成员1的定义;成员2的定义；...;成员n的定义；}==；==
2. 定义结构体变量
* 先后法，(先定义结构体类型名，再定义结构体变量)，推荐使用
* 同时法，( 定义结构体的同时定义结构体变量)
* 省略法，结构体类型和结构体变量同时定义，类型名可省略
* 注：结构体变量在内存中占有的字节数为各成员占用字节数总和</p>
<ol>
<li>结构体变量的初始化及引用</li>
<li>在定义结构体变量的同事可以将各成员的初值按顺序放在一对花括号中，来进行对结构体变量的初始化。若初值个数多于成员个数则出错，若初值个数少于成员个数，则多余成员自动赋0.</li>
<li>结构体变量不能整体引用，只能引用他的成员（同数组类似）<ul>
<li>引用结构体成员的方式：</li>
<li>结构体变量名.成员名</li>
<li>其中(.)为成员运算符。</li>
</ul>
</li>
<li>
<p>指向结构体数据类型的指针</p>
<ol>
<li>指向结构变量的指针<ul>
<li>当p指向结构体变量aa时，有如下三种方式访问成员</li>
<li>p-&gt;num</li>
<li>(*p).num</li>
<li>aa.num</li>
</ul>
</li>
<li>指向结构体数组的指针</li>
</ol>
</li>
<li>
<p>注意事项</p>
<ul>
<li>可以用结构体变量的成员作为实参，他与普通变量作为实参的方法是一样的。 </li>
<li>用结构体变量作为实参时，要求形参必须是同一结构类型的的变量，传递后形参与实参各对应成员值是一样的。</li>
<li>也可以用结构体类型的地址(指针变量或数组)作为实参，要求形参必须是同一结构体类型的指针变量或数组。只要是地址传递，则可以通过形参来改变实参的数值。</li>
</ul>
</li>
</ol>
<div class="highlight"><pre><span></span>void struct_test()
{   
    struct student
    {
        int num;
        char name[100];
        char sex;
        float score;
    };
    //定义结构体变量,并初始化及使用结构体成员变量。
    struct student stu1={1001,&quot;Maxiaoan&quot;,&#39;M&#39;,99.78};
    printf(&quot;student 1 Information.&quot;);
    printf(&quot;%d\n&quot;,stu1.num);
    printf(&quot;%.2f\n&quot;,stu1.score);
    printf(&quot;%c\n&quot;,stu1.sex);
    printf(&quot;%s\n&quot;,stu1.name);
    /*定义结构体变量,并初始化及定义指向结构体的指针变量，并
    使用指向运算符（-&gt;）调用结构体成员
     */
    struct student stu2={1002,&quot;Huxiaona&quot;,&#39;W&#39;,99.99};
    struct student *stu2_p = &amp;stu2;
    printf(&quot;student 2 Information.&quot;);
    printf(&quot;%d\n&quot;,stu2_p-&gt;num);
    printf(&quot;%s\n&quot;,stu2_p-&gt;name);
    printf(&quot;%c\n&quot;,stu2_p-&gt;sex);
    printf(&quot;%.2f\n&quot;,stu2_p-&gt;score);
    /*定义指向结构体数组的指针变量，并
    使用指向运算符（-&gt;）调用结构体成员
     */
     struct student stu[3]={
         {1001,&quot;Huxiaona&quot;,&#39;W&#39;,79.99},
         {1002,&quot;Huxiaona&quot;,&#39;M&#39;,89.99},
         {1003,&quot;Huxiaona&quot;,&#39;W&#39;,99.99},
         };
    struct student *stu_p=stu;
    for(int i=0;i&lt;3;i++)
        {
            //方式1
            printf(&quot;%d,%s,%c,%.2f\n&quot;,
            stu_p-&gt;num,stu_p-&gt;name,stu_p-&gt;sex,stu_p-&gt;score);
            //方式2
            printf(&quot;%d,%s,%c,%.2f\n&quot;,
            (*stu_p).num,(*stu_p).name,(*stu_p).sex,(*stu_p).score);
            stu_p++;    //指针下移一个元素
            }
}
</pre></div>


<p>二、链表
* （1） 链表概述
* 键表是一种数据结构，它采用动态分配存储单元方式．它能够有效地节省存储空间〈同数组比较〉
* 链表都有个“头指针”变量（ head ），它用于指向链表中的第个元素〈即用于存放链表中第一个元素的地址〉．链表中的元素称为“结点”，链表中的所有结点都是结构体类型，且同一链表中的结点都是同一结构体类型．每个结点都应包括数据部分和下个结点地址两部分内容．链表的最后一个元素&lt;结点&gt;称为链尾，它不再指向其他结点〈即该结点的指针成员值为Null)
* 链表的访问都是从指针变量从头结点开始．
* (2）动态存储分配函数<stdlib.h>
    1. malloc()函数
    2. 格式:malloc(size)
    3. 作用是在内存的动态存储去中分配一个长度为size个字节的连续的空间，函数返回值为一个指向分配域起始地址的指针，若分配失败，则返回NULL
    4. 例如，开辟一个用于存放struct student数据类型的内存空间，并让p指向该存储空间,举例如下：
    5. struct student <em>p= (struct student </em>)malloc(sizeof(sturct student)),其中，(struct studeng *)为强制类型转换
    6. free()函数
    7. 格式free(p)
    8. 上述两个函数配套使用
作用是释放malloc()分配的内存．</p>
<ul>
<li>(3)链表操作</li>
</ul>
<p>(1)建立动态链表
* 假定输入的输入的成员为0则表示结束</p>
<p>(2)访问链表</p>
<p>(3)链表结点的删除
如：
- a、请将结点b从链表中删除
- b、请将结点d 从链表中删除。
- c、请将a结点从链表中删除并让head重新指向链表的第一个结点
- c、请注结翩翩表中删除并让h咀新指向链表的第一个结</p>
<p>(4)增加结点
- a、请将结点c插入结点d与b之间，插入后a、c、b形成新链表。(先连后断)
- b、请将结点c插到已有链表的末尾，使其成为新链表的居结点
-c、请将结点c插到最前面，使其成为新链表的头结点并让head指向新的结点。</p>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">(</span><span class="kt">int</span> <span class="n">argc</span><span class="p">,</span> <span class="k">const</span> <span class="kt">char</span> <span class="o">*</span> <span class="n">argv</span><span class="p">[])</span> <span class="p">{</span>
    <span class="c1">//构造结构体，构造结点</span>
    <span class="k">struct</span> <span class="n">node</span><span class="p">{</span>
        <span class="kt">int</span> <span class="n">data</span><span class="p">;</span>
        <span class="k">struct</span> <span class="n">node</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span> <span class="c1">//必须要有指向自己的指针</span>
    <span class="p">};</span>

    <span class="k">struct</span> <span class="n">node</span> <span class="o">*</span><span class="n">head</span><span class="p">,</span><span class="o">*</span><span class="n">p</span><span class="p">,</span><span class="o">*</span><span class="n">q</span><span class="p">;</span>    <span class="c1">//定义结构体指针变量</span>
    <span class="n">p</span><span class="o">=</span><span class="p">(</span><span class="k">struct</span> <span class="n">node</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">node</span><span class="p">));</span><span class="c1">//申请空间</span>
    <span class="n">p</span><span class="o">-&gt;</span><span class="n">data</span><span class="o">=</span><span class="mi">10</span><span class="p">;</span> <span class="c1">//访问成员</span>
    <span class="n">head</span><span class="o">=</span><span class="n">p</span><span class="p">;</span>     <span class="c1">//头指针</span>
    <span class="n">q</span><span class="o">=</span><span class="p">(</span><span class="k">struct</span> <span class="n">node</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="k">struct</span> <span class="n">node</span><span class="p">));</span>
    <span class="n">q</span><span class="o">-&gt;</span><span class="n">data</span> <span class="o">=</span> <span class="mi">20</span><span class="p">;</span>
    <span class="n">q</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="nb">NULL</span><span class="p">;</span>
    <span class="n">p</span><span class="o">-&gt;</span><span class="n">next</span> <span class="o">=</span> <span class="n">q</span><span class="p">;</span>    <span class="c1">//形成链表，将p指向一下一个节点的地址。</span>


    <span class="k">struct</span> <span class="n">node</span> <span class="o">*</span><span class="n">pp</span><span class="p">;</span>
    <span class="kt">int</span> <span class="n">sum</span> <span class="o">=</span><span class="mi">0</span><span class="p">;</span>
    <span class="n">pp</span><span class="o">=</span><span class="n">head</span><span class="p">;</span>
    <span class="k">while</span><span class="p">(</span><span class="n">pp</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">sum</span><span class="o">+=</span><span class="n">pp</span><span class="o">-&gt;</span><span class="n">data</span><span class="p">;</span>
        <span class="n">pp</span><span class="o">=</span><span class="n">pp</span><span class="o">-&gt;</span><span class="n">next</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;hello,world</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">);</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;sum=%d</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">,</span><span class="n">sum</span><span class="p">);</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="n">getchar</span><span class="p">();</span>
    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>


<h4>数据集的存储访问方式：</h4>
<ol>
<li>定义为数组（连续，随机访问，顺序存储）；</li>
<li>定义为链表：<ol>
<li>首先要定义结点(需要用结构体来构造，结点有内容和地址两部分组成）</li>
<li>动态机制；</li>
<li>生成链表， 、定义为链表（不连续存储、顺序访问））</li>
<li>然后是操作（插入、修改，访问），</li>
</ol>
</li>
</ol>
<p>三、共用体类型</p>
<ul>
<li>共用体中的所有成员共用同一段内存〈所有成员的起始地址都是一样的</li>
<li>格式：union 共用体类型名 {成员列表};</li>
<li>注：</li>
<li>(I)成员列表为定义该共用体的成员，成员定义的方式与普通变量的方式一样．</li>
<li>(2)成员刑表必须用对花括号括起，最后有分号．</li>
<li>
<p>(3)共用体名可以省略</p>
</li>
<li>
<p>1、共用体类型的定义</p>
</li>
<li>(1)先定义类，再定义变量</li>
<li>(2)定义类型的同时，定义变量</li>
<li>(3)直接定义变量
union data {char ch[10];float s;} a1;</li>
</ul>
<p>注：
由于所有共用体类型中所有成员共享一段内存，所以共用体类型类别所占的字节数等于该共用体类型中占用字节数最多的成员所占的字节数。sizeof(a1)=&gt;10</p>
<p>2、共用体交量的引用
- (1)不能整体引用共用体变量，只能引用其成员．
    引用格式： 共用体变量名成员名
- (2)同类型成员共享值
- (3)在内存中整型数据的二进制数低8位占用前面一个字节，高八位占用后面一个字节
- (4)共用体成员之间可以相互赋值；
- (5)共用体变量的地址与各成员的地址都是相同的
- (6)在定义共用时，金额以对其进行初始化，但是只能用个初值，且必须用花括号将初值括起来，相当于给第一个给第一个成员赋值
- (7)共同体、结构体的成员也可以是共用体或者结构体
- (8)计算共用体字节数。
* 如整数255，在内存中的存储形式为：
* 11111111 00000000。
* 一个字符型数据占用一个字节，对于数组来说前面元素占用前面的字节。</p>
<h1>四、typedef</h1>
<ul>
<li>用typedef给已存在类型取别名</li>
<li>格式：typedef 已存在类型名   新类型名</li>
<li>typedef int INTEGER</li>
<li>INTEGER a[10],b;int a[10],b;</li>
<li>typedef int ARR[10]; ARR a,b[2];=&gt;a[10],b[2][10]</li>
<li>typedef char <em>POINT; POINT p1,</em>p1;</li>
<li></li>
</ul>
<div class="highlight"><pre><span></span><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;stdlib.h&gt;</span><span class="cp"></span>
<span class="cp">#include</span> <span class="cpf">&lt;string.h&gt;</span><span class="cp"></span>
<span class="cp">#define N 40</span>

<span class="k">typedef</span> <span class="k">struct</span> <span class="n">student</span>
<span class="p">{</span>
    <span class="kt">int</span> <span class="n">studentID</span><span class="p">;</span>
    <span class="kt">char</span> <span class="n">studentName</span><span class="p">[</span><span class="mi">20</span><span class="p">];</span>
    <span class="kt">char</span> <span class="n">studentSex</span><span class="p">;</span>

    <span class="k">struct</span> <span class="n">student</span> <span class="o">*</span><span class="n">next</span><span class="p">;</span>
<span class="p">}</span><span class="n">STUDENT</span><span class="p">;</span>

<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">void</span><span class="p">);</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span>
<span class="p">{</span>

    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">head</span><span class="o">=</span><span class="n">Build</span><span class="p">();</span>
    <span class="n">printf</span><span class="p">(</span><span class="s">&quot;the first student name is %s &quot;</span><span class="p">,</span> <span class="n">head</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">);</span>

    <span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>


<span class="n">STUDENT</span> <span class="o">*</span> <span class="nf">Build</span><span class="p">(</span><span class="kt">void</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu1</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu2</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">stu3</span><span class="o">=</span><span class="p">(</span><span class="n">STUDENT</span> <span class="o">*</span><span class="p">)</span><span class="n">malloc</span><span class="p">(</span><span class="k">sizeof</span><span class="p">(</span><span class="n">STUDENT</span><span class="p">));</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu1</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1001</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;马小安&quot;</span><span class="p">);</span>
        <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu2</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1002</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;胡小娜&quot;</span><span class="p">);</span>
        <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;W&#39;</span><span class="p">;</span>
    <span class="p">}</span>
    <span class="k">if</span><span class="p">(</span><span class="n">stu3</span><span class="o">!=</span><span class="nb">NULL</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentID</span><span class="o">=</span><span class="mi">1003</span><span class="p">;</span>
        <span class="n">strcpy</span><span class="p">(</span><span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentName</span><span class="p">,</span><span class="s">&quot;马宇恒&quot;</span><span class="p">);</span>
        <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">studentSex</span><span class="o">=</span><span class="sc">&#39;M&#39;</span><span class="p">;</span>
    <span class="p">}</span>


    <span class="n">STUDENT</span> <span class="o">*</span><span class="n">head</span><span class="p">;</span>
    <span class="n">head</span><span class="o">=</span><span class="n">stu1</span><span class="p">;</span>
    <span class="n">stu1</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="n">stu2</span><span class="p">;</span>
    <span class="n">stu2</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="n">stu3</span><span class="p">;</span>
    <span class="n">stu3</span><span class="o">-&gt;</span><span class="n">next</span><span class="o">=</span><span class="nb">NULL</span><span class="p">;</span>
    <span class="k">return</span> <span class="n">head</span><span class="p">;</span>
<span class="p">}</span>
</pre></div>
    </div><!-- /.entry-content -->

  </article>
</section>
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>